
#include "..\UnitTest\ut_features.h"
#include "..\UnitTest\ut_runner.h"

//#include "cx_platform_base.h"
#include "cx_list.h"
#include "cx_memory.h"

static QINT Compare_Int (QVOID * pFirst, QVOID * pSecond)
{
	QINT* p1 = (QINT*)pFirst;
	QINT* p2 = (QINT*)pSecond;
	if (*p1 == *p2) return 0;
	else if (*p1 > *p2) return 1;
	else return -1;
}

/*************************************************************************
* UT CASE
*************************************************************************/

UT_CASE( __test_enter )
{
	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_new_test )
{
	QList* list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_newEx_test )
{
	QList* list = cx_list_newEx(QTRUE, NULL);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_count_test )
{
	QINT* obj = QNULL;
	QList* list = QNULL;
	list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( list->m_iCount == 0, UT_RESULT_FAIL );

	obj = (QINT*)cx_malloc(sizeof(QINT));
	cx_list_append(list, obj);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_append_test )
{
	int* obj = QNULL;
	QList* list = QNULL;
	QBOOL ret = QFALSE;
	list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj = (QINT*)cx_malloc(sizeof(QINT));
	ret = cx_list_append(list, obj);
	UT_EXPECT_OR_RETURN( ret == QTRUE, UT_RESULT_FAIL );

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}

	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_removeAll_test )
{
	int* obj = QNULL;
	QList* list = QNULL;
	list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj = (int*)cx_malloc(sizeof(int));
	cx_list_append(list, obj);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	cx_list_removeAll(list);
	UT_EXPECT_OR_RETURN( list->m_iCount == 0, UT_RESULT_FAIL );

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}

	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_setElementAt_test )
{
	int* obj_i = NULL;
	int* obj_o = NULL;
	QBOOL ret = QFALSE;

	QList* list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj_i = (QINT*)cx_malloc(sizeof(QINT));
	*obj_i = 5;
	cx_list_append(list, obj_i);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 6;
	ret = cx_list_setElementAt(list, 0, obj_i);
	UT_EXPECT_OR_RETURN( ret == QTRUE, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	if (list != QNULL)
	{
		cx_list_releaseRef(list);
	}
	
	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_getElementAt_test )
{
	int* obj_i = QNULL;
	int* obj_o = QNULL;
	QList* list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 5;
	cx_list_append(list, obj_i);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );
	if (list != QNULL)
	{
		cx_list_releaseRef(list);
	}

	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_insertElementAt_test )
{
	int* obj_i = NULL;
	int* obj_o = NULL;
	QBOOL ret = QFALSE;
	
	QList* list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 5;
	cx_list_append(list, obj_i);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 6;
	ret = cx_list_insertElementAt(list, 0, obj_i);
	UT_EXPECT_OR_RETURN( ret == QTRUE, UT_RESULT_FAIL );

	UT_EXPECT_OR_RETURN( list->m_iCount == 2, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 1);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}

	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_removeElementAt_test )
{
	int* obj_i = NULL;
	int* obj_o = NULL;
	QList* list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 5;
	cx_list_append(list, obj_i);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (int*)cx_list_removeElementAt(list, 0);
	UT_EXPECT_OR_RETURN( list->m_iCount == 0, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	cx_free(obj_o);

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_find_test )
{
	int* obj_i = NULL;
	int* obj_o = NULL;
	QINT element = 0;
	QINT ret = -1;
	QList* list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 5;
	cx_list_append(list, obj_i);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	obj_i = (QINT*)cx_malloc(sizeof(int));
	*obj_i = 6;
	cx_list_append(list, obj_i);
	UT_EXPECT_OR_RETURN( list->m_iCount == 2, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 1);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	element = 5;
	ret = cx_list_find(list, &element, (void**)&obj_o, Compare_Int);
	UT_EXPECT_OR_RETURN( ret == 0, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	if (list != QNULL)
	{
		cx_list_releaseRef(list);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_list_setElementAtEx_test )
{
	int* obj_i = NULL;
	int* obj_o = NULL;
	int* obj_old = NULL;
	QBOOL ret = QFALSE;

	QList* list = cx_list_new(QTRUE);
	UT_EXPECT_OR_RETURN( list != NULL, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 5;
	cx_list_append(list, obj_i);
	UT_EXPECT_OR_RETURN( list->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	obj_i = (int*)cx_malloc(sizeof(int));
	*obj_i = 6;
	ret = cx_list_setElementAtEx(list, 0, obj_i, (void**)&obj_old);
	UT_EXPECT_OR_RETURN( ret == QTRUE, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( *obj_old == 5, UT_RESULT_FAIL );

	obj_o = (int*)cx_list_getElementAt(list, 0);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	if (list != NULL)
	{
		cx_list_releaseRef(list);
	}

	return UT_RESULT_PASS;
}

UT_CASE( __test_leave )
{
	return UT_RESULT_PASS;
}

/*************************************************************************
* UT GROUP
*************************************************************************/

UT_GROUP( AcceptanceTest_CX_LIST )
{
	UT_ADD_CASE( __test_enter ),
	UT_ADD_CASE( __cx_list_new_test ),
	UT_ADD_CASE( __cx_list_newEx_test ),
	UT_ADD_CASE( __cx_list_count_test ),
	UT_ADD_CASE( __cx_list_append_test ),
	UT_ADD_CASE( __cx_list_removeAll_test ),
	UT_ADD_CASE( __cx_list_setElementAt_test ),
	UT_ADD_CASE( __cx_list_getElementAt_test ),
	UT_ADD_CASE( __cx_list_insertElementAt_test ),
	UT_ADD_CASE( __cx_list_removeElementAt_test ),
	UT_ADD_CASE( __cx_list_find_test ),
	UT_ADD_CASE( __cx_list_setElementAtEx_test ),
	UT_ADD_CASE( __test_leave ),

	UT_LAST_CASE
};
